package school.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import school.domain.Category;
import school.domain.Goods;
import school.mapper.CategoryMapper;
import school.mapper.GoodsMapper;
import school.support.BaseController;
import school.support.pojo.GoodsPo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@RequestMapping("admin/goods")
@Controller
public class AdminGoodsController extends BaseController {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 列表
     *
     * @param info
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(GoodsPo info, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        if (info.getName() != null) {
            criteria.andCondition("name like '%" + info.getName() + "%'");
        }

        PageHelper.startPage(pageNum, pageSize);
        PageInfo<GoodsPo> pageInfo = new PageInfo<>(goodsMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "admin/goods/list";
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        model.addAttribute(goods);
        List<Category> categoryList = categoryMapper.selectAll();
        model.addAttribute("categoryList", categoryList);
        return "admin/goods/detail";
    }

    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        goodsMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    /**
     * 修改信息
     *
     * @param goods
     * @return
     */
    @RequestMapping("update")
    public String update(Goods goods, MultipartFile icon) {
        if (!icon.isEmpty()) {
            String path = saveFile(icon);
            goods.setImg(path);
        }
        goodsMapper.updateByPrimaryKeySelective(goods);
        return refresh();
    }

    @PostMapping("add")
    public String add(Goods goods, MultipartFile icon) {
        if (!icon.isEmpty()) {
            String path = saveFile(icon);
            goods.setImg(path);
        }
        goods.setAddtime(new Date());
        goodsMapper.insertSelective(goods);
        return "redirect:list";
    }

    @GetMapping("add")
    public String add(Model model) {
        List<Category> categoryList = categoryMapper.selectAll();
        model.addAttribute("categoryList", categoryList);
        return "admin/goods/add";
    }


}
